install.packages("readxl")
install.packages("readstata13")
library(ggplot,foreign,plyr,reshape2,readxl,readstata14,readstata13, lubridate)
require(dplyr,tidyr,lubridate,readxl)
install.packages("ggplot2")
library(ggplot2)

rm(list = ls())

SD <- read.dta13('sdbydecadeALL.dta')
SD1 <- read.dta13('irregonly.dta')
lablist<-as.vector(c("1940-49","1950-59","1960-69","1970-79","1980-89","1990-99","2000-08"))
lablist2<-as.vector(c("0%", "10%", "20%", "30%", "40%", "50%", "60%", "70%"))

##FREQ (Fig 2)

dev.off()

par(mar=c(4,5,2,5))
par(pin=c(5,2.5))
par(oma = c(1, 1, 1, 1))

plot(SD$year,SD$cleans,pch = 1,lty=5,type = "l", lwd=2, col="gray60",ylim=c(0,60), axes=FALSE, ann=FALSE)
axis(1, las=1, c("1940","1950","1960","1970","1980","1990","2000"),labels = lablist, cex.axis=.80)
axis(2, las=1, at=10*0:60, cex.axis=.80)
title(ylab= "% of All Civil Wars",cex.axis=.80)
box()
lines(SD$year, SD$depop,lty=3,type = "l", lwd=2, col="gray30")
lines(SD$year, SD$resett,lty=1,type = "l", lwd=2, col="black")
par(fig = c(0, 1, 0, 1), oma = c(0, 0, 0, 0), mar = c(0, 0, 0, 0), new = TRUE)
plot(0, 0, type = "n", bty = "n", xaxt = "n", yaxt = "n")
legend("bottom",col=c("gray60","gray30","black"),xpd = TRUE, horiz = FALSE, inset = c(0,0),bty="n",lty=c(5,3,1),lwd=2,ncol = 3, legend=c("Cleansing","Depopulation","Forced Relocation"))

##MASS KILLING (Fig 5)

dev.off()

par(mfrow=c(1,2))

plot(SD$year,SD$nosd,pch = 1,lty=5,type = "l", lwd=2, col="gray20",ylim=c(0,90), axes=FALSE, ann=FALSE)
axis(1, las=1, c("1940","1950","1960","1970","1980","1990","2000"),labels = lablist, cex.axis=.80)
axis(2, las=1, at=10*0:90, cex.axis=.80)
title(ylab= "% of All Civil Wars",cex.axis=.80)
box()
lines(SD$year, SD$cleans,lty=3,type = "l", lwd=2, col="gray60")
lines(SD$year, SD$resett,lty=1,type = "l", lwd=2, col="black")
lines(SD$year, SD$mk,lty=4,type = "l", lwd=2, col="gray50")

lablist<-as.vector(c("1940-49","1950-59","1960-69","1970-79","1980-89","1990-99","2000-08"))
lablist2<-as.vector(c("0%", "10%", "20%", "30%", "40%", "50%", "60%", "70%"))
SD1 <- read.dta13('irregonly.dta')
lablist<-as.vector(c("1940-49","1950-59","1960-69","1970-79","1980-89","1990-99","2000-08"))
lablist2<-as.vector(c("0%", "10%", "20%", "30%", "40%", "50%", "60%", "70%"))

##MASS KILLING - IRREGULAR WARS ONLY

plot(SD1$year,SD1$nosd,pch = 1,lty=5,type = "l", lwd=2, col="gray20",ylim=c(0,90), axes=FALSE, ann=FALSE)
axis(1, las=1, c("1940","1950","1960","1970","1980","1990","2000"),labels = lablist, cex.axis=.80)
axis(2, las=1, at=10*0:90, cex.axis=.80)
title(ylab= "% of All Irregular Wars",cex.axis=.80)
box()
lines(SD1$year, SD1$cleans,lty=3,type = "l", lwd=2, col="gray60")
lines(SD1$year, SD1$resett,lty=1,type = "l", lwd=2, col="black")
lines(SD1$year, SD1$mk,lty=4,type = "l", lwd=2, col="gray50")
par(fig = c(0, 1, 0, 1), oma = c(0, 0, 0, 0), mar = c(0, 0, 0, 0), new = TRUE)
plot(0, 0, type = "n", bty = "n", xaxt = "n", yaxt = "n")
legend("bottom",col=c("gray20","gray60","black", "gray50"),xpd = TRUE, horiz = FALSE, inset = c(0,0),bty="n",lty=c(5,3,1,4),lwd=2,ncol = 2, legend=c("No Strategic Displacement","Cleansing","Forced Relocation", "Mass Killing"))

rm(list = ls())

#Rebel groups and military troops (Fig 6) ####

uganda1 <- read.csv('ugandamilper.csv')
dev.off()

par(mar=c(4,5,2,5))
par(pin=c(5,2.5))
par(oma = c(1, 1, 1, 1))

plot(uganda1$year,uganda1$milper_nmc,pch = 1,lty=1,type = "l", lwd=2, col="black",ylim=c(40,70), axes=FALSE, ann=FALSE)
axis(1, las=2, srt=45, c("1990","1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008"),cex.axis=.75)
axis(2, las=1, at=5*0:70, cex.axis=.75)
title(ylab= "Troops (Thousands)",cex.axis=.75)
title(main= "")
box()
par(new=TRUE)
plot(uganda1$year, uganda1$rebgroups2,pch=1, lty=4,type="l", lwd=2, col="gray60", xaxt="n",yaxt="n",ylab="",xlab="",ylim=c(0,8))
axis(4, las=1, at=1*0:8, cex.axis=.75)
mtext("Number of Rebel Groups",side=4,line=3)
abline(v=1996, lty=3, lwd=2,col = "black")
abline(v=1997, lty=3, lwd=2,col = "black")
abline(v=2002, lty=3, lwd=2,col = "black")
par(fig = c(0, 1, 0, 1), oma = c(0, 0, 0, 0), mar = c(0, 0, 0, 0), new = TRUE)
plot(0, 0, type = "n", bty = "n", xaxt = "n", yaxt = "n")
legend("bottom",col=c("black","gray60","black"), xpd = TRUE, inset = c(0,0),bty="n",lty=c(1,4,3),lwd=3,ncol = 2, legend=c("Troops", "Rebel Groups","Displacement Ordered"))

### Rebel Attacks (Fig 7 and Appendix 5.3) #####

setwd("/Users/alichtenheld/Dropbox/Research Projects/Dissertation/Data/Uganda Research/Violence Data/GED")

uganda3 <- read.csv('ugandagedmonth.csv')
uganda3$date <- as.Date(uganda3$date, "%m/%d/%y")

dev.off()

par(mar=c(4,5,2,5))
par(pin=c(5,2.5))
par(oma = c(1, 1, 1, 1))

#Civilian Attacks
plot(uganda3$date,uganda3$lra_civattacks,pch = 1,lty=1,type = "l", lwd=2, col="black",ylim=c(0,60), axes=FALSE, ann=FALSE)
axis.Date(1,las=2, at=seq(min(uganda3$date), max(uganda3$date), by="1 year"), format="%Y", cex.axis=.75)
axis(2, las=1, at=10*0:60, cex.axis=.75)
title(ylab= "No. Attacks",cex.axis=.75)
title(main= "")
box()
par(new=TRUE)
lines(uganda3$date, uganda3$adf_civdattacks,lty=4,type = "l", lwd=2, col="gray60")
abline(v=as.Date("1996-06-01"), lty=2, lwd=2,col = "black")
abline(v=as.Date("1997-06-01"), lty=3, lwd=2,col = "gray60")
abline(v=as.Date("2002-06-01"), lty=2, lwd=2,col = "black")
par(fig = c(0, 1, 0, 1), oma = c(0, 0, 0, 0), mar = c(0, 0, 0, 0), new = TRUE)
plot(0, 0, type = "n", bty = "n", xaxt = "n", yaxt = "n")
legend("bottom",col=c("black","gray60", "black", "gray60"), xpd = TRUE, inset = c(0,0),bty="n",lty=c(1,4,2,3),lwd=3,ncol = 2,legend=c("By LRA", "By ADF", "Displacement Ordered (LRA)", "Displacement Ordered (ADF)"))

#Civilian Deaths
dev.off()

par(mar=c(4,5,2,5))
par(pin=c(5,2.5))
par(oma = c(1, 1, 1, 1))

plot(uganda3$date,uganda3$lra_attackdeaths,pch = 1,lty=1,type = "l", lwd=2, col="black",ylim=c(0,800), axes=FALSE, ann=FALSE)
axis.Date(1,las=2, at=seq(min(uganda3$date), max(uganda3$date), by="1 year"), format="%Y", cex.axis=.75)
axis(2, las=1, at=100*0:800, cex.axis=.75)
title(ylab= "No. Deaths",cex.axis=.75)
title(main= "")
box()
par(new=TRUE)
lines(uganda3$date, uganda3$adf_attackdeaths,lty=4,type = "l", lwd=2, col="gray50")
abline(v=as.Date("1996-06-01"), lty=2, lwd=2,col = "black")
abline(v=as.Date("1997-06-01"), lty=3, lwd=2,col = "gray50")
abline(v=as.Date("2002-06-01"), lty=2, lwd=2,col = "black")
par(fig = c(0, 1, 0, 1), oma = c(0, 0, 0, 0), mar = c(0, 0, 0, 0), new = TRUE)
plot(0, 0, type = "n", bty = "n", xaxt = "n", yaxt = "n")
legend("bottom",col=c("black","gray50", "black", "gray50"), xpd = TRUE, inset = c(0,0),bty="n",lty=c(1,4,2,3),lwd=3,ncol = 2,legend=c("By LRA", "By ADF", "Displacement Ordered (LRA)", "Displacement Ordered (ADF)"))

#Battles/Rebel Deaths (LRA)
dev.off()

par(mar=c(4,5,2,5))
par(pin=c(5,2.5))
par(oma = c(1, 1, 1, 1))

plot(uganda3$date,uganda3$lra_battles,pch = 1,lty=1,type = "l", lwd=2, col="black",ylim=c(0,120), axes=FALSE, ann=FALSE)
axis.Date(1,las=2, at=seq(min(uganda3$date), max(uganda3$date), by="1 year"), format="%Y", cex.axis=.75)
axis(2, las=1, at=20*0:120, cex.axis=.75)
title(ylab= "Battles",cex.axis=.75)
box()
par(new=TRUE)
plot(uganda3$date, uganda3$lra_rebdeaths,pch=1, lty=4,type="l", lwd=2, col="gray50", xaxt="n",yaxt="n",ylab="",xlab="",ylim=c(0,650))
axis(4, las=1, at=100*0:650, cex.axis=.75)
mtext("Fatalities",side=4,line=3)
abline(v=as.Date("1996-06-01"), lty=3,lwd=2, col = "black")
abline(v=as.Date("2002-06-01"), lty=3, lwd=2,col = "black")
par(fig = c(0, 1, 0, 1), oma = c(0, 0, 0, 0), mar = c(0, 0, 0, 0), new = TRUE)
plot(0, 0, type = "n", bty = "n", xaxt = "n", yaxt = "n")
legend("bottom",col=c("black","gray50", "black"), xpd = TRUE, inset = c(0,0),bty="n",lty=c(1,4,3),lwd=3,ncol = 2,legend=c("UPDF-LRA Battles", "LRA Fatalities", "Displacement Ordered"))

#Battles/Rebel Deaths (ADF)
uganda4 <- subset(uganda3, date > "1995-01-01")

dev.off()

par(mar=c(4,5,2,5))
par(pin=c(5,2.5))
par(oma = c(1, 1, 1, 1))

plot(uganda4$date,uganda4$adf_battles,pch = 1,lty=1,type = "l", lwd=2, col="black",ylim=c(0,25), axes=FALSE, ann=FALSE)
axis.Date(1,las=2, at=seq(min(uganda4$date), max(uganda4$date), by="1 year"), format="%Y", cex.axis=.75)
axis(2, las=1, at=5*0:25, cex.axis=.75)
title(ylab= "Battles",cex.axis=.75)
box()
par(new=TRUE)
plot(uganda4$date, uganda4$adf_rebdeaths,pch=1, lty=4,type="l", lwd=2, col="gray50", xaxt="n",yaxt="n",ylab="",xlab="",ylim=c(0,400))
axis(4, las=1, at=50*0:400, cex.axis=.75)
mtext("Fatalities",side=4,line=3)
abline(v=as.Date("1997-06-01"), lty=3, lwd=2,col = "black")
par(fig = c(0, 1, 0, 1), oma = c(0, 0, 0, 0), mar = c(0, 0, 0, 0), new = TRUE)
plot(0, 0, type = "n", bty = "n", xaxt = "n", yaxt = "n")
legend("bottom",col=c("black","gray50", "black"), xpd = TRUE, inset = c(0,0),bty="n",lty=c(1,4,3),lwd=3,ncol = 2,legend=c("UPDF-ADF Battles", "ADF Fatalities", "Displacement Ordered"))

#BY REGION
setwd("/Users/alichtenheld/Dropbox/Research Projects/Dissertation/Data/Uganda Research/Violence Data/GED")

uganda2 <- read.csv('ged_uganda.csv')

#LRA
ugandaLRAciv <- subset(uganda2, side_a == "LRA" & side_b == "Civilians")
ugandaLRAciv2 <- subset(ugandaLRAciv, adm_1 == "Gulu District" | adm_1 == "Kitgum District" | adm_1 == "Pader District" | adm_1 == "Lira District")

ugandaLRAcivno <- aggregate(list(lra_civattacks=ugandaLRAciv2$id), by=list(date=ugandaLRAciv2$year, district=ugandaLRAciv2$adm_1), FUN=length)

ugandaLRAcivdeaths <- aggregate(list(lra_attackdeaths=ugandaLRAciv2$deaths_civilians), by=list(date=ugandaLRAciv2$year, district=ugandaLRAciv2$adm_1), FUN=sum)

m1 <- merge(ugandaLRAcivno, ugandaLRAcivdeaths, all.x=TRUE)

write.csv(m1, file = "ugandaLRAregion.csv")

ugandaLRA <- read.csv('ugandaLRAregion.csv')

ggplot(ugandaLRA) +
  geom_line(aes(date, lra_civattacks, group = 1), show.legend = FALSE) + 
  geom_vline(xintercept=1995, lty=2, col="red") +
  geom_vline(xintercept=2001, lty=2, col="red") +
  facet_wrap(~district) +
  labs(title = "",
       y = "No. Attacks",
       x = "Date")

#ADF
ugandaADFciv <- subset(uganda2, side_a == "ADF" & side_b == "Civilians")
ugandaADFciv2 <- subset(ugandaADFciv, adm_1 == "Bundibugyo District" | adm_1 == "Kabarole District" | adm_1 == "Kibale District" | adm_1 == "Kasese District")

ugandaADFcivno <- aggregate(list(adf_civdattacks=ugandaADFciv$id), by=list(date=ugandaADFciv$year, district=ugandaADFciv$adm_1), FUN=length)
ugandaADFcivdeaths <- aggregate(list(adf_attackdeaths=ugandaADFciv$deaths_civilians), by=list(date=ugandaADFciv$year, district=ugandaADFciv$adm_1), FUN=sum)

m2 <- merge(ugandaADFcivno, ugandaADFcivdeaths, all.x=TRUE)

write.csv(m2, file = "ugandaADFregion.csv")

ugandaADF <- read.csv('ugandaADFregion.csv')
ugandaADF <- subset(ugandaADF, district == "Bundibugyo District" | district == "Kabarole District" | district == "Kasese District")

ggplot(ugandaADF, aes(date, adf_civdattacks)) +
  geom_line() +
  geom_vline(xintercept=1997, lty=5, col="red") +
  facet_wrap(~district) +
  labs(title = "ADF Attacks on Civilians, 1997-2002",
       y = "No. Attacks",
       x = "Date")